/*
 * Util.java
 *
 * Created on 13 de julio de 2007, 03:40
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import javax.swing.JOptionPane;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
/**
 *
 * @author mariano
 */
public class Util {
    
    public static void errorDialog( String title, String message )
    {
        JOptionPane.showMessageDialog( null, message, title, JOptionPane.ERROR_MESSAGE );
    }
    
    public static void informDialog( String title, String message )
    {
        JOptionPane.showMessageDialog( null, message, title, JOptionPane.INFORMATION_MESSAGE );
    }
    
    public static int confirmDialog( String title, String message )
    {
       return JOptionPane.showConfirmDialog( null, message, title, JOptionPane.OK_CANCEL_OPTION );
    }
    
    public static boolean fileExists(String path)
    {
        return (new File(path)).exists();
    }
    
    public static boolean dirExists(String path)
    {
        File f = new File(path); 
        return (f.exists() && f.isDirectory());
    }
    
    public static void createDir(String path)
    {
        if(!dirExists(path))
            (new File(path)).mkdirs();
    }
    
    public static String getCurrentDirectory()
    {
        return System.getProperty("user.dir");
    }
    
    public static String readTextFile(String path)
    {
        StringBuffer sb = new StringBuffer();
        
        try
        {
            BufferedReader in = new BufferedReader(new FileReader( path ));
            
            String str;
            while ((str = in.readLine()) != null)
            {
                sb.append(str);
            }
            
            in.close();
        }
        catch (IOException e)
        {
            return "";
        }
        
        return sb.toString();
    }
    
    public static String readUTF8TextFile(String path)
    {
        StringBuffer sb = new StringBuffer();
        
        try
        {
            BufferedReader in = new BufferedReader(
            new InputStreamReader(new FileInputStream(path), "UTF8"));
            
            String str;
            while((str = in.readLine()) != null)
            {
                sb.append(str);
            }
            
            return sb.toString();
            
        }
        catch (UnsupportedEncodingException e)
        {
            return "";
        }
        catch (IOException e)
        {
            return "";
        }
    }
    
    public static void writeTextFile( String path, String text ) throws FileNotFoundException, IOException
    {
        FileOutputStream fout;		
        fout = new FileOutputStream (path);
        new PrintStream(fout).print(text);
        fout.close();		
    }
    
    public static String xslTransform( String filePath, String xslPath )
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        try
        {
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer(new StreamSource(xslPath ));
            transformer.transform( new StreamSource(filePath ), new StreamResult( baos ));
        }
        catch( Exception ex )
        {
            System.err.println( "error convirtiendo a html " + ex.toString() );
            return "";
        }
         
        return baos.toString();
    }
}
